package com.example.demo.service.impl;

import com.example.demo.mapper.InquiryMapper;
import com.example.demo.model.Inquiry;
import com.example.demo.model.InquiryParts;
import com.example.demo.model.vo.InquiryAddVo;
import com.example.demo.model.vo.InquiryPartsVo;
import com.example.demo.model.vo.InquirySupplierVo;
import com.example.demo.model.vo.InquiryVo;
import com.example.demo.service.InquiryService;
import com.example.demo.util.Result;
import com.example.demo.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2018/10/26 0026.
 */
@Service
public class InquiryServiceImpl implements InquiryService {
    @Autowired
    private InquiryMapper inquiryMapper;

    @Override
    public Result<InquiryVo> selectById(Integer id) {
        //验证id是否存在
        if (id == null){
            return ResultVo.error("450","您所查询的数据不存在，请重新输入");
        }
        InquiryVo inquiryVo = inquiryMapper.selectById(id);
        //验证询价单是否存在
        if (inquiryVo == null){
            ResultVo.error("450","您所查询的信息不存在，请重新输入");
        }
        //根据id查询备件信息
        List<InquiryPartsVo> partsVoList = inquiryMapper.selectByInquiryParts(id);
        //根据id查询供应商信息
        List<InquirySupplierVo> inquirySupplierVoList = inquiryMapper.selectByInquirySupplier(id);
        inquiryVo.setPartsList(partsVoList);
        inquiryVo.setSupplierList(inquirySupplierVoList);

        return ResultVo.success(inquiryVo);
    }
//添加数据
    @Override
    public Result<InquiryVo> addInquiry(InquiryAddVo inquiryAddVo) {
        Inquiry inquiry = new Inquiry();
        BeanUtils.copyProperties(inquiryAddVo, inquiry);
        //验证数据是否合法
        Result<Inquiry> inquiryTotalResult = validateInquiry(inquiry);
        if (!inquiryTotalResult.isStatus()) {
            return ResultVo.error(inquiryTotalResult.getErrorCode(), inquiryTotalResult.getMessage());
        }
        inquiry.setInquiryNo(UUID.randomUUID().toString());
        //插入表头数据
        Date newDate = new Date();
        inquiry.setCreateTime(newDate);
        inquiry.setCreateName("江翔");
        inquiry.setCreateId(10);
        inquiry.setUpdateId(10);
        inquiry.setUpdateName("江丰年");
        inquiry.setUpdateTime(newDate);
        inquiryMapper.addInquiry(inquiry);
        //插入备件数据
        int inquiryId = inquiry.getId();
        List<InquiryPartsVo> partsList = inquiry.getPartsList();
        for (InquiryPartsVo inquiryPartsVo : inquiry.getPartsList()) {
            inquiryPartsVo.setInquiryId(inquiryId);
        }
        inquiryMapper.insertInquiryParts(partsList);
        //插入供应商数据
        List<InquirySupplierVo> supplierList = inquiry.getSupplierList();
        for (InquirySupplierVo inquirySupplierVo : inquiry.getSupplierList()) {
            inquirySupplierVo.setInquiryId(inquiryId);
        }
        inquiryMapper.insertInquirySupplier(supplierList);
        return ResultVo.success(inquiryAddVo);
//        return null;
    }

    private Result<Inquiry> validateInquiry(Inquiry inquiry) {
        //验证数据是否合法
        Result<Inquiry> validate = validatePartsCode(inquiry);
        if (!validate.isStatus()) {
            return ResultVo.error(validate.getErrorCode(), validate.getMessage());
        }
        // 2.验证备件、供应商是否存在，批量查询数据库是否存在此备件、供应商。
        Result<Inquiry> inquiryTotalResult = validatePartsExist(inquiry);
        if (!inquiryTotalResult.isStatus()) {
            return ResultVo.error(inquiryTotalResult.getErrorCode(), inquiryTotalResult.getMessage());
        }
//        3.验证备件是否存在合法的供应商，//        4.验证供应商是否有合法的备件
        Result<Inquiry> validateLegal = validateLegal(inquiry);
        if (!validate.isStatus()) {
            return ResultVo.error(validate.getErrorCode(), validate.getMessage());
        }
/*//       *//* 5.结束日期是否在开始日期之后，结束日期是否在当前时间之后
        Result<Inquiry> validateTime = validate(inquiry);
        if (!validate.isStatus()) {
            return ResultVo.error(validate.getErrorCode(), validate.getMessage());
        }
//        6.效验询价数量是否大于0
        Result<Inquiry> validateNumber = validate(inquiry);
        if (!validate.isStatus()) {
            return ResultVo.error(validate.getErrorCode(), validate.getMessage());
        }*/
        return ResultVo.success(inquiry);
    }

    private Result<Inquiry> validateLegal(Inquiry inquiry) {
        //前端传过来的备件ID集合
        Set<Integer> partIds = new HashSet<>();
        for (InquiryPartsVo inquiryPartsVo : inquiry.getPartsList()) {
            partIds.add(inquiryPartsVo.getPartsId());
        }
        // //前端传过来的供应商ID集合
        Set<Integer> supplierIds = new HashSet<>();
        for (InquirySupplierVo inquirySupplierVo : inquiry.getSupplierList()) {
            supplierIds.add(inquirySupplierVo.getSupplierId());
        }

        return ResultVo.success(inquiry);

    }

    private Result<Inquiry> validatePartsExist(Inquiry inquiry) {
        /*//页面请求过来的备件编码集合
        List<String> partCodeList = new ArrayList<>();
        for (InquiryPartsVo inquiryPartsVo : inquiry.getPartsList()) {
            partCodeList.add(inquiryPartsVo.getPartsCode());
        }
        //根据备件code集合查询备件信息,如果哪个备件不存在，提示用户该备件不存在
        List<InquiryParts> inquiryPartsList = inquiryMapper.queryByPartCodes(partCodeList);
        //根据备件编码集合查询数据库响应回来的编码集合
        Set<String> returnCodeSet = new HashSet<>();
        for (InquiryParts inquiryParts : inquiryPartsList) {
            returnCodeSet.add(inquiryParts.getPartsCode());
        }
        if (partCodeList.size() != inquiryPartsList.size()) {
            partCodeList.removeAll(returnCodeSet);
            return ResultVo.error("500", "不存在的备件编码为：" + partCodeList);
        }*/
        return ResultVo.success(inquiry);

    }

    //验证备件编码是否重复
    private Result<Inquiry> validatePartsCode(Inquiry inquiry) {
        List<InquiryPartsVo> inquiryPartsVoList = inquiry.getPartsList();

        //优化代码，准确提示给用户哪个备件重复
        List<String> partList = new ArrayList<>();
        for (InquiryPartsVo inquiryPartsVo : inquiryPartsVoList) {
            partList.add(inquiryPartsVo.getPartsCode());
        }
        List<String> codeList = new ArrayList<>();
        Set<String> errorCodeList = new HashSet<>();
        for (String code : partList) {
            if (!codeList.contains(code)) {
                codeList.add(code);
            } else {
                errorCodeList.add(code);
            }
        }
        if (errorCodeList.size() > 0) {
            return ResultVo.error("500", "存在重复的备件信息，重复的备件为" + errorCodeList);
        }

        return ResultVo.success(inquiry);
    }


}
